import { createApp } from "vue";
import ElementPlus from "element-plus";
import * as ElementPlusIconsVue from "@element-plus/icons-vue";
import "element-plus/dist/index.css";
import zhCn from "element-plus/es/locale/lang/zh-cn";
import { ElMessage } from "element-plus";

import App from "./App.vue";
import router from "./router";
import "./style.css";
import "./styles/index.css";

const app = createApp(App);

// 注册所有图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}

app.use(router);
app.use(ElementPlus, {
  locale: zhCn,
});

// 开发环境下记录API调用
if (import.meta.env.DEV) {
  console.log("DEV 环境启动，启用详细日志记录");

  // 监控所有API请求
  const originalFetch = window.fetch;
  window.fetch = function (...args) {
    console.log("API请求:", args);
    return originalFetch.apply(this, args).then((response) => {
      console.log("API响应:", response);
      return response;
    });
  };
}

// 添加全局错误处理
app.config.errorHandler = (err, instance, info) => {
  console.error("全局错误:", err);
  console.info("错误发生在组件:", instance);
  console.info("错误信息:", info);

  // 可以在这里添加全局错误提示
  ElMessage.error("操作出错，请刷新页面重试");
};

// 添加未捕获错误处理
window.addEventListener("unhandledrejection", (event) => {
  console.error("未处理的Promise拒绝:", event.reason);
});

app.mount("#app");
